package com.example.hospitalsystemclient.ui.dashboard;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hospitalsystemclient.Dataclass.Doctor;
import com.example.hospitalsystemclient.R;

import java.util.List;

import android.widget.AdapterView;
import android.widget.TextView;

public class DoctorAdapter extends RecyclerView.Adapter<DoctorAdapter.DoctorViewHolder> {

    private List<Doctor> doctorList;
    private OnItemClickListener listener;

    public DoctorAdapter(List<Doctor> doctorList) {
        this.doctorList = doctorList;
    }

    public interface OnItemClickListener {
        void onItemClick(Doctor doctor);
    }

    public DoctorAdapter(List<Doctor> doctorList, OnItemClickListener listener) {
        this.doctorList = doctorList;
        this.listener = listener;
    }

    public void updateData(List<Doctor> newDoctorList) {
        this.doctorList = newDoctorList;
        notifyDataSetChanged(); // 通知数据已更改
    }

    @NonNull
    @Override
    public DoctorViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_doctor, parent, false);
        return new DoctorViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DoctorViewHolder holder, int position) {
        Doctor doctor = doctorList.get(position);
        holder.tvDocName.setText(doctor.getDocName());
        holder.tvDocAge.setText("年龄: " + doctor.getDocAge());
        holder.tvDocGender.setText("性别: " + doctor.getDocGender());
        holder.tvDocLevel.setText("级别: " + doctor.getDocLevel());
        holder.tvOfficeName.setText("科室: " + doctor.getOfficeName());

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(doctor);
            }
        });
    }

    @Override
    public int getItemCount() {
        return doctorList.size();
    }

    public static class DoctorViewHolder extends RecyclerView.ViewHolder {
        TextView tvDocName, tvDocAge, tvDocGender, tvDocLevel, tvOfficeName;

        public DoctorViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDocName = itemView.findViewById(R.id.tvDocName);
            tvDocAge = itemView.findViewById(R.id.tvDocAge);
            tvDocGender = itemView.findViewById(R.id.tvDocGender);
            tvDocLevel = itemView.findViewById(R.id.tvDocLevel);
            tvOfficeName = itemView.findViewById(R.id.tvOfficeName);
        }
    }
}